package com.rgz.controller.wgController;




import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.rgz.pojo.User;
import com.rgz.service.WgService.UserService;


@Controller
public class UninterceptController {
	@Autowired
	private UserService userService;
	
	/**
	 * 跳转登陆的界面
	 */
	@RequestMapping("whd_login")
	public String whd_login() {
		return "whd_login";
	}
	/**
	 * 车险自助查勘理赔
	 */
	@RequestMapping("user_WGSelfHelp")
	public String user_WGSelfHelp(HttpServletRequest request,Model model) {
		User user = (User)request.getSession().getAttribute("user");
		String userphone = user.getUserphone();
			Map map = userService.selectByStarts(userphone);
			model.addAttribute("map", map);
			return "WGSelfHelp";
	}
	
	
	/**
	 * 获取验证码
	 * 传值前端
	 */
	@RequestMapping(value="/login_sms",produces="application/json;charset=utf-8")
	@ResponseBody
	public Object login_in(Model model,String yzms) {
		int round = (int) (Math.random()*8999)+1000;
			/*String testUsername = "a751130153"; //在短信宝注册的用户名
	        String testPassword = "wanghandong520"; //在短信宝注册的密码
	        String testPhone = yzms;
	        
	        String testContent = "【万千购】您的验证码是"+round+",５分钟内有效。若非本人操作请忽略此消息。"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到
	       
	        String httpUrl = "http://api.smsbao.com/sms";
	 
	        StringBuffer httpArg = new StringBuffer();
	        httpArg.append("u=").append(testUsername).append("&");
	        httpArg.append("p=").append(SMSUtils.md5(testPassword)).append("&");
	        httpArg.append("m=").append(testPhone).append("&");
	        httpArg.append("c=").append(SMSUtils.encodeUrlString(testContent, "UTF-8"));
	       
	        String result = SMSUtils.request(httpUrl, httpArg.toString());
			
			System.out.println(result);*/
			String result="0";
	        String jsonString =null;
	        if("0".equals(result)){
	        	System.out.println("验证码："+round);
	        	jsonString = JSON.toJSONString(round);
	        }else{
	        	jsonString = JSON.toJSONString("发送验证码异常，请检查后重试");
	        }
			return jsonString;
	        }
	
	/**
	 * 用于短信验证登陆
	 * @param phone	电话
	 * @param yzms	验证码
	 * @param hiddenno
	 * @return
	 */
	@RequestMapping("login_in")
	public String login_in(String phone,String yzms,String hiddenno,HttpServletRequest request){
		if(hiddenno != null && yzms !=null){
			if(yzms.equals(hiddenno)){
			//向数据库中增加或者直接使用
			User user = userService.UserLogin(phone);
			//登陆的用户，存进session
			request.getSession().setAttribute("user", user);
			return "ccx_home_page";
			}
		}
		return "whd_login";
	}
	
	
	/**
	 * ajax提交验证错误时清除前端验证码
	 * @param yzms		输入的验证码
	 * @param hiddenno	发送的验证码
	 * @return
	 */
	@RequestMapping(value="/login_in2",produces="application/json;charset=utf-8")
	@ResponseBody
	public Object login_in2(String yzms,String hiddenno) {
		String jsonString =null;
			if(!yzms.equals(hiddenno)){
				jsonString = JSON.toJSONString("验证码错误！");
			}else{
				jsonString = JSON.toJSONString("成功");
			}
		return jsonString;
	}
	
	
	
	
}
